<template>
  <div>
    <div>
      <el-table v-loading="loading" :data="tableData"
        style="width: 100%">
        <el-table-column prop="doctorName" label="医生姓名" width="180">
        </el-table-column>
        <el-table-column prop="startTime" label="预约时间" width="200" align="center">
        </el-table-column>
        <el-table-column prop="status" label="时间段状态" v-if="false">
        </el-table-column>
        <el-table-column prop="num" label="挂号量" align="center">
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="tag" color="blue" round icon="el-icon-edit" @click="toBook(scope.row)">预约</el-button>
            <el-button size="mini" type="tag" color="blue" round icon="el-icon-delete" @click="cancelBook(scope.row)">取消预约</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="block">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
  </div>
</template>
<style>
      
</style>

<script>
import { bookList, toBook, cancelBook } from '../../api/bookRecord'
import { deptmentList } from '../../api/deptment'
import { getUserInfo } from '../../utils/userInfoUtil'
import { doctorinfoList } from '../../api/doctor'

export default {
  name: 'BookRecord',
  data() {
    return {
      // 遮罩层
      loading: true,
      form: {},
      tableData: [],
      deptmentList: [],
      total: null,
      currentPage: 1,
      pageSize: 5,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        deptValue: ''
      },
      doctorinfoList: [],
      statusTime: [
        {
          value: '0',
          label: '可预约'
        },
        {
          value: '1',
          label: '已约满'
        }
      ],
    }
  },
  created() {
    this.getDepementsList();
    this.getDoctorinfoList();
    this.getList();
  },
  methods: {
    getDoctorinfoList() {
      doctorinfoList().then(res => {
        this.doctorinfoList = res.data.rows;
      })
    },
    getList() {
      this.loading = true;
      bookList(this.queryParams).then(res => {
        this.tableData = res.data.rows;
        for (let i = 0; i < this.tableData.length; i++) {
          for (let j = 0; j < this.doctorinfoList.length; j++) {
            if (this.tableData[i].userId == this.doctorinfoList[j].id) {
              this.tableData[i].doctorName = this.doctorinfoList[j].name;
            }
          }
        }
        this.total = res.data.total;
        this.loading = false;
      })
    },
    /** 查询部门列表 */
    getDepementsList() {
      deptmentList().then(res => {
        for (let i = 0; i < res.data.data.length; i++) {
          if (res.data.data[i].createBy === 'jerry') {
            this.deptmentList.push(res.data.data[i]);
          }
        }
      })
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.queryParams.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.tableData = [];
      this.currentPage = val;
      this.queryParams.pageNum = val;
      this.getList();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = this.currentPage;
      this.getList();
    },
    /** 预约操作 */
    toBook(row) {
      row.patientId = getUserInfo().id;
      toBook(row).then(res => {
        if (res.data.code == 200) {
          this.$message.success(res.data.msg);
          this.getList();
        } else {
          this.$message.error(res.data.msg);
        }
      });
    },
    /** 取消预约操作 */
    cancelBook(row) {
      row.patientId = getUserInfo().id;
      cancelBook(row).then(res => {
        if (res.data.code == 200) {
          this.$message.success(res.data.msg);
          this.getList();
        } else {
          this.$message.error(res.data.msg);
        }
      });
    },
  }
}
</script>

